home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / sysutil.c < prev    next >
C/C++ Source or Header  |  1993-07-26  |  6KB  |  300 lines

  1. /*
  2. *                sysutil.c
  3. *
  4. * This is the repository of most of the commonly used system dependent code
  5. * in the Citadel utilities.
  6. */
  7. /*
  8. *                history
  9. *
  10. * 87Feb12 HAW  Created.
  11. */
  12. #define SYSTEM_DEPENDENT
  13. #include "ctdl.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "stdarg.h"
  18. #include "fcntl.h"
  19. #include <math.h>
  20. #include <ctype.h>
  21. #include <time.h>
  22. #include <proto/exec.h>
  23. #include <dos/dos.h>
  24. #include <pragmas/dos_pragmas.h>
  25. #include "exec/memory.h"
  26. #include "exec/ports.h"
  27. #include "exec/exec.h"
  28. #include "stdarg.h"
  29. #include "dos.h"
  30. #include "sysutil.h"
  31.  
  32. char bigbuffer[MAXTEXT];
  33.  
  34.  
  35. /*
  36. *                Contents
  37. *
  38. *    getUtilDate()        gets date from system for utilities
  39. *    strCmpU()        strcmp(), but ignoring case distinctions
  40. *    UtilGetch()        gets a character
  41. *    NEUtilGetch()        gets a character, no echo
  42. */
  43. char *READ_ANY="rb";
  44. char *WRITE_ANY="wb";
  45. char *R_W_ANY="r+b";
  46. char *W_R_ANY="w+b";
  47. char *APPEND_TEXT="a";
  48. char *READ_TEXT="r";
  49. char *WRITE_TEXT="w";
  50. char *APPEND_ANY  = "a+b";
  51. char  *monthTab[13] =
  52.   {
  53.   "", "Jan", "Feb", "Mar",
  54.   "Apr", "May", "Jun",
  55.   "Jul", "Aug", "Sep",
  56.   "Oct", "Nov", "Dec"
  57.  
  58.   };
  59. /*
  60. * getUtilDate()
  61. *
  62. * This function gets the raw date from MSDOS.
  63. */
  64. void getUtilDate(year, month, day, hours, minutes)
  65. int *year, *month, *day, *hours, *minutes;
  66.   {
  67.   int mon, seconds, milli;
  68.   getRawDate(year, &mon, day, hours, minutes, &seconds, &milli);
  69.   *year -= 1900;
  70.   *month = (int)monthTab[mon];
  71.  
  72.   }
  73. void getRawDate(int *year, int *month, int *day, int *hours, int *minutes,
  74. int *seconds, int *milli)
  75.   {
  76.   long t;
  77.   struct tm *v;
  78.   time(&t);
  79.   v        = localtime(&t);
  80.   *year    = v->tm_year + 1900;
  81.   *month   = v->tm_mon + 1;
  82.   *day     = v->tm_mday;
  83.   *hours   = v->tm_hour;
  84.   *minutes = v->tm_min;
  85.   *seconds = v->tm_sec;
  86.   *milli   = 0;
  87.  
  88.   }
  89.  
  90. void totalBytes(long *size, FILE *fd)
  91.   {
  92.   long oldpos;                  /* old position of this file */
  93.   oldpos = ftell(fd);           /* save old position */
  94.   fseek(fd,0l,SEEK_END);       /* go to end of file */
  95.   *size = ftell(fd);            /* get current position */
  96.   fseek(fd,oldpos,SEEK_SET);    /* back to begining */
  97.   }
  98.  
  99. /*
  100. * strCmpU()
  101. *
  102. * This is strcmp(), but ignoring case distinctions.  Yes, someday we should
  103. * replace it with stricmp(). In fact, this shouldn't be in here - it's not
  104. * system dependent.
  105. */
  106. int strCmpU(s, t)
  107. char s[], t[];
  108.   {
  109.   int  i;
  110.   i = 0;
  111.   while (toUpper(s[i]) == toUpper(t[i]))
  112.     {
  113.     if (s[i++] == '\0')  return 0;
  114.  
  115.     }
  116.   return  toUpper(s[i]) - toUpper(t[i]);
  117.  
  118.   }
  119. /*
  120. * UtilGetch()
  121. *
  122. * This will get a character from console.
  123. */
  124. int UtilGetch()
  125.   {
  126.   int c;
  127.   c = getchar();
  128.   putchar(c == '\r' ? '\n' : c);
  129.   return c;
  130.  
  131.   }
  132. /*
  133. * NEUtilGetch()
  134. *
  135. * This will get a character from console, non-echo mode.
  136. */
  137. int NEUtilGetch()
  138.   {
  139.   int c;
  140.   c = getchar();
  141.   return c;
  142.  
  143.   }
  144. /*
  145. * safeopen()
  146. *
  147. * This opens a file.
  148. */
  149. FILE *safeopen(name, mode)
  150. char *name, *mode;
  151.   {
  152.   return fopen(name, mode);
  153.  
  154.   }
  155. char *mtab[] =
  156.   {
  157.   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  158.   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  159.  
  160.   };
  161. /*
  162. * LastOn()
  163. *
  164. * This function will return a human string representing the given date.
  165. */
  166. char *LastOn(long lastdate, char shortstyle)
  167.   {
  168.   struct tm *data;
  169.   static char buffer[40];
  170.   char *m;
  171.   /* 0l represents Never in our scheme */
  172.   if (lastdate == 0l) return "Never";
  173.   data = localtime(&lastdate);
  174.   if (!shortstyle)
  175.       {
  176.     civTime(&data->tm_hour, &m);
  177.     sprintf(buffer, "%d%s%02d @ %d:%02d %s", data->tm_year,
  178.     mtab[data->tm_mon], data->tm_mday, data->tm_hour, data->tm_min, m);
  179.  
  180.     }
  181.   else
  182.   sprintf(buffer, "%d%s%02d", data->tm_year, mtab[data->tm_mon],
  183.   data->tm_mday);
  184.   return buffer;
  185.  
  186.   }
  187. /*
  188. * civTime()
  189. *
  190. * This converts military time to Civilian time.
  191. */
  192. void civTime(int *hours, char **which)
  193.   {
  194.   if (*hours >= 12)
  195.   *which = "pm";
  196.   else
  197.   *which = "am";
  198.   if (*hours >= 13)
  199.   *hours -= 12;
  200.   if (*hours == 0)
  201.   *hours = 12;
  202.  
  203.   }
  204.  
  205. /*
  206.  * ChkNtoStr()
  207.  *
  208.  * This is a required testing function for list handling.  Returns pointer to
  209.  * char.
  210.  */
  211. void *ChkNtoStr(NumToString *d1, NumToString *d2)
  212. {
  213.     if (d1->num == d2->num) return d1->string;
  214.     return NULL;
  215. }
  216.  
  217. /*
  218.  * FreeNtoStr()
  219.  *
  220.  * This frees a data node for archiving for later use.
  221.  */
  222. void FreeNtoStr(NumToString *d)
  223. {
  224.     free(d->string);
  225.     free(d);
  226. }
  227.  
  228. /*
  229.  * EatNMapStr()
  230.  *
  231.  * This Eats a string formatted as "<digit> <string>" for list handling.
  232.  */
  233. void *EatNMapStr(char *line)
  234. {
  235.     char    *s;
  236.  
  237.     if ((s = strchr(line, ' ')) == NULL)
  238.     return NULL;
  239.  
  240.     return NtoStrInit(atoi(line), s+1, 0, FALSE);
  241. }
  242.  
  243. /*
  244. * NtoStrInit()
  245. *
  246. * This is a utility function to generate temporary or permanent data nodes
  247. * for searching or adding to Archive-type lists.
  248. */
  249. NumToString t =    {    0, NULL    };
  250. void *NtoStrInit(int num, char *str, int num2, char needstatic)
  251.   {
  252.   NumToString *temp;
  253.   if (needstatic)
  254.     {
  255.     temp = &t;
  256.     if (temp->string != NULL) free(temp->string);
  257.  
  258.     }
  259.   else  temp = (NumToString *) GetDynamic(sizeof *temp);
  260.   temp->num  = num;
  261.   temp->num2 = num2;
  262.   temp->string = strdup(str);
  263.   return (void *) temp;
  264.  
  265.   }
  266. /*
  267.  * putFLChar()
  268.  *
  269.  * This is used to upload files.
  270.  * returns: ERROR on problems else TRUE.
  271.  */
  272. FILE *upfd;
  273.  
  274. int putFLChar(int c)
  275. {
  276.     if (fputc(c, upfd) != EOF)    return TRUE;
  277.     /* else */            printf("Write error: %d\n", ferror(upfd));
  278.                 return ERROR;
  279. }
  280.  
  281.  
  282. int (*ToFileWork)(int s) = putFLChar;
  283. /************************************************************************/
  284. /*      ToFile() sends the data to file pointed at by upfd              */
  285. /************************************************************************/
  286. void ToFile(char *format, ...)
  287.   {
  288.   va_list argptr;
  289.   int i;
  290.   va_start(argptr, format);
  291.   vsprintf(bigbuffer, format, argptr);
  292.   va_end(argptr);
  293.   for (i = 0; bigbuffer[i]; i++) (*ToFileWork)(bigbuffer[i]);
  294.   (*ToFileWork)(0);      /* Send NULL since it did before */
  295.  
  296.   }
  297. void Do_Stack_Check(void);
  298. void Do_Stack_Check() {}
  299. void splitF(FILE*fp, char *format, ...) {}
  300.